package com.dqp.cslggroup.School;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.BaseActivity;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.UI.electricityHistoryAdapter;
import com.dqp.cslggroup.Util.ToastUtil;
import com.dqp.cslggroup.bean.roomElectricity;

import java.util.List;
import java.util.Objects;

public class electricityHistory extends BaseActivity {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.three_text);
        initCmp();
        initListView();

    }

    private void initCmp() {
        Toolbar toolbar = findViewById(R.id.include_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        TextView seting = findViewById(R.id.seting);
        TextView cjfx = findViewById(R.id.cjfx);
        TextView fxbl = findViewById(R.id.fxbl);
        TextView cj = findViewById(R.id.cj);
        cjfx.setText("宿舍");
        fxbl.setText("剩余电量");
        cj.setText("查询时间");
        seting.setText(getResources().getString(R.string.icon_delete));
        seting.setOnClickListener(v -> ToastUtil.showToast("长按清空"));
        seting.setVisibility(View.VISIBLE);
        seting.setOnLongClickListener(v -> {
            MyApplication.getDaoInstant().getRoomElectricityDao().deleteAll();
            initListView();
            return true;
        });
        listView = findViewById(R.id.tip_list);
    }

    @SuppressLint("SetTextI18n")
    private void initListView() {
        List<roomElectricity> roomElectricityList = MyApplication.getDaoInstant().getRoomElectricityDao().loadAll();
        electricityHistoryAdapter adapter = new electricityHistoryAdapter(roomElectricityList, this);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
